package com.yun.zf.system.security.filter;

import com.yun.zf.common.admin.domain.LoginUser;
import com.yun.zf.system.security.service.TokenService;
import com.yun.zf.system.security.token.WxAppletAuthenticationToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * token过滤器 验证token有效性
 * 
 * @author ruoyi
 */
@Component
@Slf4j
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
{
    @Autowired
    private TokenService tokenService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException
    {

        log.info("当前方法请求{}",request.getMethod());
        // 处理浏览器的预请求,放行option
        if (request.getMethod().equals("OPTIONS")) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST,GET,PUT,OPTIONS,DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            // 这里如果在前端设置了一些头部信息，都需要再这里添加进来
            response.setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,Authorization,token,isLoading,city");
            log.info("已经放行啦---------------------");
            return;
        }
        // 验证token
        LoginUser loginUser = tokenService.getLoginUser(request);
       // log.info("jwt验证登录用户信息-------{}", JSONObject.toJSONString(loginUser));
        if (loginUser!=null&& "wx".equals(loginUser.getLoginType())){
            tokenService.verifyToken(loginUser);
            WxAppletAuthenticationToken wxToken = new WxAppletAuthenticationToken(loginUser,null,null);
            SecurityContextHolder.getContext().setAuthentication(wxToken);
        }
        if (loginUser!=null&&"iphone".equals(loginUser.getLoginType()))
        {
            tokenService.verifyToken(loginUser);
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            // 登录用户信息放入session
            //ServletUtils.getSession().setAttribute("loginUser",loginUser);
        }
        chain.doFilter(request, response);
    }
}
